package com.lzb.compressmaster;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sztle.admodule.outstatus.AppParam;
import com.sztle.admodule.outstatus.AppParamBuilder;
import com.sztle.admodule.outstatus.AppStatusProcessor;

import java.util.List;

public class BaseApplication extends Application {
    public static Context context;

    private static boolean isBackground = false;
    private static int activityCount = 0;
    // 添加标识：是否跳过启动页跳转（用于系统权限页面返回时）
    private static boolean skipSplashOnNextStart = false;

    /**
     * 设置是否跳过下次启动页跳转
     * 用于从系统权限页面或应用详情页面返回时避免跳转到启动页
     */
    public static void setSkipSplashOnNextStart(boolean skip) {
        skipSplashOnNextStart = skip;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        context = this;

        //todo list 初始化并传入渠道、各个广告id及其他所需id
        AppParam appParam = new AppParamBuilder()
                .setChannel(BuildConfig.FLAVOR)//商店渠道
                .setDebug(BuildConfig.DEBUG) // 是否是debug模式,正式发布一定是要false
                .setGremoreAppId("5752241")// gremore聚合appid
                .setUMengAppKey("690322ac644c9e2c206a67a6") // 友盟appkey
                .setAdsparkAppId("856598")// AdSpark的appid
                .setSplashAdId("103700340")// 开屏广告id
                .setInterstitialAdId("103699180")// 插屏广告id
                .setRewardedVideoAdId("103700151")// 激励视频广告id
                .setNativeAdId("103699960")// 原生广告id
                .build();
        AppStatusProcessor.statusStartApp(this, appParam);

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                activityCount++;
                if (isBackground && !(activity instanceof SplashActivity)) {
                    // 检查是否需要跳过启动页跳转
                    if (skipSplashOnNextStart) {
                        skipSplashOnNextStart = false; // 重置标志
                    } else {
                        Intent intent = new Intent(activity, SplashActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("isFromAwake", true);
                        activity.startActivity(intent);
                        activity.overridePendingTransition(0, 0);
                    }
                }
                isBackground = false;
                AppStatusProcessor.setBackground(false);
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                if (activity instanceof BaseNoModelActivity) {
                    AppStatusProcessor.setTopActivity(activity);
                }
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                activityCount--;
                if (activityCount <= 0) {
                    isBackground = true;
                    activityCount = 0;
                    AppStatusProcessor.setBackground(true);
                }
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });

    }

}